bd6b08
@@ -38,6 +38,8 @@
 import org.apache.hive.service.cli.TableSchema;
 import org.apache.hive.service.cli.thrift.TCLIService;
 import org.apache.hive.service.cli.thrift.TCLIServiceConstants;
+import org.apache.hive.service.cli.thrift.TCloseOperationReq;
+import org.apache.hive.service.cli.thrift.TCloseOperationResp;
 import org.apache.hive.service.cli.thrift.TColumnDesc;
 import org.apache.hive.service.cli.thrift.TFetchOrientation;
 import org.apache.hive.service.cli.thrift.TFetchResultsReq;
@@ -316,7 +318,11 @@
public void close() throws SQLException {
     if (this.statement != null && (this.statement instanceof HiveStatement)) {
       HiveStatement s = (HiveStatement) this.statement;
       s.closeClientOperation();
+    } else {
+      // for those stmtHandle passed from HiveDatabaseMetaData instead of Statement
+      closeOperationHandle(stmtHandle);
     }
+
     // Need reset during re-open when needed
     client = null;
     stmtHandle = null;
@@ -324,6 +330,20 @@
public void close() throws SQLException {
     isClosed = true;
   }
 
+  private void closeOperationHandle(TOperationHandle stmtHandle) throws SQLException {
+    try {
+      if (stmtHandle != null) {
+        TCloseOperationReq closeReq = new TCloseOperationReq(stmtHandle);
+        TCloseOperationResp closeResp = client.CloseOperation(closeReq);
+        Utils.verifySuccessWithInfo(closeResp.getStatus());
+      }
+    } catch (SQLException e) {
+      throw e;
+    } catch (Exception e) {
+      throw new SQLException(e.toString(), "08S01", e);
+    }
+  }
+
   /**
    * Moves the cursor down one row from its current position.
    *
